package jk.framework.spring.cloud.canary.loadbalancer.configure;

import jk.framework.spring.cloud.canary.loadbalancer.comonpent.CanaryRoundRobinLoadBalancer;
import jk.framework.spring.cloud.canary.loadbalancer.comonpent.LoadBalanceRouteContextFactory;
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @author cuichao
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnDiscoveryEnabled
public class CanaryLoadBalancerConfigure {

    @Bean
    public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment,
                                                                                   LoadBalancerClientFactory loadBalancerClientFactory,
                                                                                   LoadBalanceRouteContextFactory contextFactory) {
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new CanaryRoundRobinLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class),
                name,contextFactory);
    }

}
